home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / dec92.zip / 1012107A < prev    next >
Text File  |  1992-05-26  |  3KB  |  148 lines

  1. /*        LISTING 7. DFAFCNS.C                */
  2. /* Collection of functions to support the DFA         */
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8. #include "dfa.h"
  9.  
  10. /****************************************************/
  11. /* The first few routines are support routines for  */
  12. /*      the state machine.                          */
  13. /* The second set are default target routines       */
  14. /* The third set are default action routines        */
  15. /****************************************************/
  16.  
  17. /****************************************************/
  18. /* STATE-MACHINE-SPECIFIC SUPPORT ROUTINES          */
  19. /****************************************************/
  20.  
  21. // Init the state machine object so tokenizer can be
  22. //      extracted. Setup a DFA_CLASS to allow tokenizer
  23. //    to run without the state machine.
  24. void InitStateMachine(
  25.     DFA_CLASS *p,       // user class to init
  26.     void *statetbl,     // user's state table
  27.     char *tokenizer,    // user-defined tokenizer
  28.     FILE *infile,       // input file
  29.     char *tokelims)     // token delimiters
  30. {
  31.     // Set the user's state table address
  32.     p->tbl = statetbl;
  33.  
  34.     // Set the input file
  35.     p->infile = infile;
  36.  
  37.     // Set default, or user's override, as tokenizer
  38.     if (tokenizer == NULL) {
  39.         p->get_token = get_token;
  40.     }
  41.     else {
  42.         p->get_token = tokenizer;
  43.     }
  44.  
  45.     // Set the tokenizer's delimiters
  46.     p->delim = tokelims;
  47.  
  48.     // Set the internal working variables
  49.     p->movest = move;    // state transition fcn
  50.     p->ndx = 0;            // initial state
  51.     p->oflag = 0;        // init to clear
  52.  
  53.     memset(p->workbuf, 0, MAXBUFLEN);
  54.     p->wp = &(p->workbuf[0]);
  55.     p->cp = 0;
  56.  
  57.     return;
  58. }
  59.  
  60.  
  61. // Update state table index as the machine moves
  62. //      to the next state.
  63. short move(
  64.     DFA_CLASS *o,       // ptr to current dfa
  65.     short value)        // GOOD or BAD to next state
  66. {
  67.     CURRENT_STATE = value ? GOODSTATE : BADSTATE;
  68.     return(CURRENT_STATE);
  69. }
  70.  
  71. /***************************/
  72. /* DEFAULT TARGET ROUTINES */
  73. /***************************/
  74. // Check if end of buffer (record) has been reached
  75. short isEOF(
  76.    char *token)
  77. {
  78.     return(token[0] == '\0');
  79. }
  80.  
  81. // Check if end of buffer (record) has been reached
  82. short isEOR(
  83.    char *token)
  84. {
  85.     return(token[0] == EOR);
  86. }
  87.  
  88. // Check if a Field Separator has been found
  89. short isFS(
  90.    char *token)
  91. {
  92.     return(token[0] == FS);
  93. }
  94.  
  95. // Check if a Group Separator has been found
  96. short isGS(
  97.    char *token)
  98. {
  99.     return(token[0] == GS);
  100. }
  101.  
  102. // Check if a Left Brace has been found
  103. short isLBR(
  104.    char *token)
  105. {
  106.     return(token[0] == LBR);
  107. }
  108.  
  109. // Check if a Group Separator has been found
  110. short isRBR(
  111.    char *token)
  112. {
  113.     return(token[0] == RBR);
  114. }
  115.  
  116.  
  117. /***************************/
  118. /* DEFAULT ACTION ROUTINES */
  119. /***************************/
  120.  
  121. // Print out an error message
  122. short errmsg(
  123.    char *token)
  124. {
  125.     fprintf(stderr,
  126.         "Error in state machine for token '%s'\n",
  127.             token);
  128.     return(ERR);
  129. }
  130.  
  131. // Function that does nothing;
  132. // Allows moving to next state
  133. short skip(
  134.    char *token)
  135. {
  136.     IGNORE(token);
  137.     return(1);
  138. }
  139.  
  140. // Block tokenizer so same token can be reused
  141. short block(
  142.    char *token)
  143. {
  144.     IGNORE(token);
  145.     need_token = NO;
  146.     return(1);
  147. }
  148.